﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormNXB : Form
    {
        private CXulyNhaxuatban xuly;
        string ma;

        public FormNXB()
        {
            InitializeComponent();
        }

        private void FormNXB_Load(object sender, EventArgs e)
        {
            xuly = new CXulyNhaxuatban();
            hienthidsnxb(xuly.GetdsNhaXuatBan());
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            FormEditNXB f = new FormEditNXB(0,"");
            f.ShowDialog();
            hienthidsnxb(xuly.GetdsNhaXuatBan());
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvnxb.SelectedItems)
            {
                ma = lvi.SubItems[0].Text;
            }
            FormEditNXB f = new FormEditNXB(1,ma);
            f.ShowDialog();
            hienthidsnxb(xuly.GetdsNhaXuatBan());
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            if (txtten.Text == "")
            {
                hienthidsnxb(xuly.GetdsNhaXuatBan());
            }
            else
            {
                List<NhaXuatBan> dsNXB = xuly.TimNhaXuatBanten(txtten.Text);          
                if (dsNXB != null)
                {
                    lvnxb.Items.Clear();
                    foreach (NhaXuatBan nxb in dsNXB)
                    {
                        if (nxb.NhaXuatBanStatus == "live")
                        {
                            ListViewItem lvi = lvnxb.Items.Add(nxb.NhaXuatBanNo);
                            lvi.SubItems.Add(nxb.NhaXuatBanName);
                            lvi.SubItems.Add(nxb.NhaXuatBanAddress);
                            lvi.SubItems.Add(nxb.NhaXuatBanPhoneNumber);
                            lvi.SubItems.Add(nxb.NhaXuatBanEmail);
                        }
                    }
                }
                else
                {
                    lvnxb.Items.Clear();
                    MessageBox.Show("Không có nhà xuất bản này !!!");
                }
            }
        }

        private void hienthidsnxb(List<NhaXuatBan> ds)
        {
            lvnxb.Items.Clear();
            foreach (NhaXuatBan nxb in ds)
            {
                if (nxb.NhaXuatBanStatus == "live")
                {
                    ListViewItem lvi = lvnxb.Items.Add(nxb.NhaXuatBanNo);
                    lvi.SubItems.Add(nxb.NhaXuatBanName);
                    lvi.SubItems.Add(nxb.NhaXuatBanAddress);
                    lvi.SubItems.Add(nxb.NhaXuatBanPhoneNumber);
                    lvi.SubItems.Add(nxb.NhaXuatBanEmail);
                }
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            foreach(ListViewItem lvi in lvnxb.SelectedItems)
            {
                string ma = lvi.SubItems[0].Text;
                xuly.XoaNhaXuatBan(ma);
            }
            hienthidsnxb(xuly.GetdsNhaXuatBan());
        }
    }
}
